<?php
function p($obj){
	echo "<pre>";
	print_r($obj);
	echo "</pre>";
}
class DrwebUpdater {
	var $connectId;
	var $loginResult;
	var $localBasesList;
	var $remoteBasesList;
	var $diffBasesList;
	var $tmpDir;
	var $archiveExt;
	var $deleteAfterExtract;
	var $basesPath;

	function __construct(){
		$this->tmpDir = "d:\\soft\\drweb\\updater.tmp\\";
		$this->archiveExt = ".zip";
		$this->deleteAfterExtract = false;
		$this->basesPath = "pub/drweb/bases/";
	}
	function update($ftp_server = null, $install_dir = null, $version = null){
		$this->connect($ftp_server);
		$this->login();
		$this->getLocalBasesList($install_dir, $version);
		$this->getRemoteBasesList($version);
		$this->makeDiffBasesList();
		$this->download();
		$this->extract($install_dir);
		$this->close();
	}

	function connect($ftp_server){
		echo "Connecting to " . $ftp_server ." ......";
		$this->connectId = ftp_connect($ftp_server, 0, 50) or die("Cannot connect to ". $ftp_server);
		echo "ok.\n";
		return $this->connectId;
	}

	function login(){
		echo "Logging in ......";
		$this->loginResult = ftp_login($this->connectId, "anonymous", "drwebUpdater@") or die("Login error.");
		echo "ok.\n";
		return $this->loginResult;
	}

	function getLocalBasesList($install_dir, $version){
		$basesList = array_merge(
		glob($install_dir . "\\drw" . $version . "*.vdb" ),
		glob($install_dir . "\\dwn" . $version . "*.vdb" ),
		glob($install_dir . "\\dwr" . $version . "*.vdb" ),
		glob($install_dir . "\\drwebase.vdb" ),
		glob($install_dir . "\\drwnasty.vdb" ),
		glob($install_dir . "\\drwrisky.vdb" ),
		//glob($install_dir . "\\drwtoday.vdb" ),
		glob($install_dir . "\\dwntoday.vdb" ),
		glob($install_dir . "\\dwrtoday.vdb" ),
		glob($install_dir . "\\drweb32.dll" ),
		glob($install_dir . "\\vrcpp.dll" )
		);
		//clearstatcache();
		foreach($basesList as $v) {
			$baseFileName = explode(".", basename($v));
			//$this->localBasesList[$baseFileName[0]] = filectime($v);
			$this->localBasesList[] = $baseFileName[0];
		}
		asort($this->localBasesList);
		//p($this->localBasesList);
	}

	function getRemoteBasesList($version){
		ftp_chdir($this->connectId, $this->basesPath . $version . "/" );
		$basesList = ftp_nlist($this->connectId, ".");
		foreach($basesList as $v) {
			//$this->remoteBasesList[basename($v,$this->archiveExt)] = ftp_mdtm($this->connectId, $v);
			$this->remoteBasesList[] = basename($v,$this->archiveExt);

		}
		asort($this->remoteBasesList);
		//p($this->remoteBasesList);
	}

	function makeDiffBasesList() {
		$this->diffBasesList = array_diff($this->remoteBasesList, $this->localBasesList);

		/*foreach($this->remoteBasesList as $k=>$v) {
			if( ($v > $this->localBasesList[$k]) || !array_key_exists($k, $this->localBasesList) ){
			$this->diffBasesList["local:". date(DATE_COOKIE, $this->localBasesList[$k]).", remote:".date(DATE_COOKIE,$v)] = $k;
			//$this->diffBasesList[$this->localBasesList[$k]] = $k;
			}
			}*/
		if(count($this->diffBasesList) == 0) {
			echo "Drweb is up to date already.";
		}
		//p($this->diffBasesList);
		return $this->diffBasesList;
	}

	function download() {
		
		foreach($this->diffBasesList as $v) {
			echo "Downloading " . $v . $this->archiveExt . "......";
			if( ftp_get($this->connectId, $this->tmpDir . $v . $this->archiveExt, $v . $this->archiveExt, FTP_BINARY) ) {
				echo "ok.\n";
			}
			else {
				echo "error.\n";
			}
		}
	}

	function extract($install_dir){
		$downloadedBases = glob($this->tmpDir . "*.zip" );
		$zip = new ZipArchive;
		foreach ($downloadedBases as $archive){
			if ($zip->open($archive) === TRUE) {
				/*for($i=0; $i < $zip->numFiles; $i++){
					$fileInfo = $zip->statIndex($i);
					unlink($install_dir."\\".$fileInfo['name']);
					}*/
				if($zip->extractTo($install_dir)){
					echo $archive . " extracted.\n";
				}
				else {
					echo $archive . " extraction failed.\n";
				}
				$zip->close();

				if($this->deleteAfterExtract) {
					unlink($archive);
				}
			}
			else {
				echo $archive . "opening failed. \n";
			}
		}
	}

	function close(){
		ftp_close($this->connectId);
	}
}
$updater = new DrwebUpdater();
$updater->update("ftp.drweb.com", "d:\\soft\\drweb", "444");
?>